
var user = require("user");
var handler = require("handler")
var dataManager = require("data_manager")
var roleType = [
    {
        name: "野蛮人",
        baseNum: [1, 2, 3, 4],
        baseMuli: [10, 5, 4, 3],
        desc: "野蛮人：战斗能力高，战斗能力高，战斗能力高"
    },
    {
        name: "战士",
        baseNum: [3, 2, 1, 4],
        baseMuli: [5, 10, 5, 3],
        desc: "战士：战斗能力高，战斗能力高，战斗能力高"
    },
    {
        name: "智者",
        baseNum: [1, 2, 3, 4],
        baseMuli: [10, 5, 4, 3],
        desc: "智者：战斗能力高，战斗能力高，战斗能力高"
    },
    {
        name: "幸运儿",
        baseNum: [3, 2, 1, 4],
        baseMuli: [5, 10, 5, 3],
        desc: "幸运儿：战斗能力高，战斗能力高，战斗能力高"
    }
];

cc.Class({
    extends: cc.Component,
    properties: {
        // birthNode
        birthRoot: cc.Node,
        birthNode: [cc.Node],

        areaBg: [cc.Sprite],
        barBg: [cc.Sprite],
        barSpriteFrame: [cc.SpriteFrame],
        areaTips: [cc.Node],

        // createNode
        creatRoot: cc.Node,
        editbox: cc.EditBox,
        skillLabel: cc.Label,	
        attributeLabel: [cc.Label],
        attributePlusLabel: [cc.Label],
        roleSelectBtn: [cc.Button],
        tips: cc.Label,
    },

    onLoad: function() {
        this._step = 0;
        this._index = -1;
        this._birthArea = -1;

        this.roleType = 0;
        this.skillNum = 20;
        this.skillSelected = [0, 0, 0, 0];

        this.initialize();
    },

    initialize: function() {
        dataManager.init();
        handler.getAccount();
        var account = user.account;

        handler.getPlayer();

        console.log("user.valid = " + user.valid);
        if (!user.valid) {
            this._refresh();
        } else {
            this.gotoNextScene();
        }
    },

    start: function() {
        this.scheduleOnce(this._refresh, 0.5);
    },

    onDestroy: function() {

    },

    onClickSelectBirth: function(eventid, index) {
        if (this._index == index) return;
        if (this._index >= 0 && this._index <3) {
            this.areaTips[this._index].active = false;
            this.birthNode[this._index].getComponent(cc.Button).interactable = true;
            this.barBg[this._index].spriteFrame = this.barSpriteFrame[0];
        }

        this._index = parseInt(index);
        this.areaTips[this._index].active = true;
        this.birthNode[this._index].getComponent(cc.Button).interactable = false;
        this.barBg[this._index].spriteFrame = this.barSpriteFrame[1];
        
        this._birthArea = this._index;
    },

    onClickSelected: function() {
        user.birthArea = this._birthArea;
        ++this._step;
        this._refresh();
    },

    onClickOK: function() {
        if (this.editbox.string == "") {
            alert("请输入昵称");
            return;
        }
        user.power = this._power;
        user.wit = this._wit;
        user.strength = this._strength;
        user.luck = this._luck;
        user.nick = this.editbox.string;
        ++this._step;
        
        var attributes = {
            power: user.power,
            wit: user.wit,
            luck: user.luck,
            strength: user.strength
        };
        handler.createPlayer(user.nick, JSON.stringify(attributes));
        this._refresh();
    },

    _refresh: function() {
        if (this._step == 0) {
            this._refreshBirthNode();

        } else if (this._step == 1) {
            this._refreshRoleNode();

        } else {
            this.gotoNextScene();

        }
    },

    _refreshBirthNode: function() {
        this.birthRoot.active = true;
        this.creatRoot.active = false;

        this.onClickSelectBirth(null, 0);
    },

    _refreshRoleNode: function() {
        this.birthRoot.active = false;
        this.creatRoot.active = true;

        this.skillLabel.string = this.skillNum;
        for (var i = 0; i < this.attributeLabel.length; ++i) {
            var baseNum = this._getBaseData(this.roleType, "baseNum")[i];
            var baseMuli = this._getBaseData(this.roleType, "baseMuli")[i];
            if (i == 0) {
                this.attributeLabel[i].string = (baseNum + baseMuli * this.skillSelected[i]);
                this.attributePlusLabel[i].string = baseMuli * this.skillSelected[i];
                this._power = (baseNum + baseMuli * this.skillSelected[i]);
            } else if(i == 1) {
                this.attributeLabel[i].string = (baseNum + baseMuli * this.skillSelected[i]);
                this.attributePlusLabel[i].string = baseMuli * this.skillSelected[i];
                this._strength = (baseNum + baseMuli * this.skillSelected[i]);
            } else if(i == 2) {
                this.attributeLabel[i].string = (baseNum + baseMuli * this.skillSelected[i]);
                this.attributePlusLabel[i].string = baseMuli * this.skillSelected[i];
                this._wit = (baseNum + baseMuli * this.skillSelected[i]);
            } else if(i == 3) {
                this.attributeLabel[i].string = (baseNum + baseMuli * this.skillSelected[i]);
                this.attributePlusLabel[i].string = baseMuli * this.skillSelected[i];
                this._luck = (baseNum + baseMuli * this.skillSelected[i]);
            }
        }

        this.tips.string = this._getBaseData(this.roleType, "desc");
    },

    _getBaseData: function(roleIndex, key) {
        return roleType[roleIndex][key];
    },

    onClickPlus: function(eventid, index) {
        if (this.skillNum <= 0) return;
        
        var _index = parseInt(index);
        if (this.skillSelected[_index] >= 0) {
            this.skillNum--;
            this.skillSelected[_index]++;
        }
        this._refreshRoleNode();
    },

    onClickDec: function(eventid, index) {
        var _index = parseInt(index);
        if (this.skillSelected[_index]) {
            if (this.skillSelected[_index] > 0) {
                this.skillNum++;
                this.skillSelected[_index]--;
            }
            this._refreshRoleNode();
        }
    },

    onClickRole: function(eventid, index) {
        if (this.roleType == index) return;
        this.roleType = parseInt(index);
        this._refreshRoleNode();
    },

    gotoNextScene: function() {
        cc.director.loadScene("main_scene");
    }

});